extends Panel
	
@onready var hand_card_container = $HandCardContainer
@onready var ok_button = $OkButton
#选牌模式取消按钮
signal card_table_card_select_window_cancel
#选牌模式确认按钮
signal card_table_card_select_window_confirm
var card_max = 999
var card_min = 0
var table_hand_card_container

func _ready():
	Events.card_table_card_selected.connect(_on_card_table_card_selected)

func _on_card_table_card_selected(card_view):
	if hand_card_container.has_card(card_view):
		move_card(card_view, table_hand_card_container)
	else:
		add_card(card_view)

func move_card(card_view, other_card_container):
	card_view.card_container.card_in_preview = null
	card_view.card_container.card_in_select = null
	card_view.reparent(other_card_container)
	card_view.card_container.do_organize_cards()
	card_view.card_container = other_card_container
	other_card_container.do_organize_cards()

func add_card(card_view:CardView):
	var count = hand_card_container.get_all_card().size()
	move_card(card_view, hand_card_container)
	if count > card_max:
		var cv = hand_card_container.get_all_card().pop_front()
		Events.card_table_card_selected.emit(cv)
	count = hand_card_container.get_all_card().size()
	if count <= card_max and count >= card_min:
		ok_button.disabled = false
	else:
		ok_button.disabled = true


func _on_ok_button_pressed():
	var count = hand_card_container.get_all_card().size()
	if count <= card_max and count >= card_min:
		card_table_card_select_window_confirm.emit(hand_card_container.get_all_card())
		self.visible = false
		self.card_max = 999
		self.card_min = 0
		

func _on_cancel_button_pressed():
	card_table_card_select_window_cancel.emit(hand_card_container.get_all_card())
	self.visible = false
	self.card_max = 999
	self.card_min = 0

func clear_card():
	hand_card_container.clear_card()
